// pages/components/yxItem/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item:{
      type: Object
    },
    index:{
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    addFunc(event){
      var dataset=event.currentTarget.dataset
      let item=this.properties.item
      if(dataset.flag){
        item.num++
      }else{
        if(item.num>0){
          item.num--
        }
      }
      this.setData({
        item
      })
      let list = wx.getStorageSync('yx-shoplist')
      list[this.properties.index]=item
      wx.setStorageSync('yx-shoplist',list)
      this.setShopCart()
    },
    setShopCart(){
      let list=wx.getStorageSync('yx-shopcart')
      let item=this.properties.item
      if(list&&list.length>0){
        let bol=false,idx
        list.forEach((i,index)=>{
          if(i.id==item.id){
            if(item.num==0){
              idx=index
            }else{
              i.num=item.num
            }
            bol=true
          }
          if((index+1)==list.length){
            if(!bol){
              list.push(item)
            }else{
              if(idx>=0){
                list.splice(idx,1)
              }
            }
          }
        })
      }else{
        list=[item]
      }
      wx.setStorageSync('yx-shopcart',list) 
      this.triggerEvent('getShopCart')
    },
  }
})
